new Vue({
  el: "#app",
  data() {
    return {
      upLoadFilesList: new Array(new Array()),
      fmfileList: [],
      VarietyName: "",
      form: {},
      form2: [
        {
          GradeName: "",
        },
      ],
      form3: [],
      rules: {
        //   VarietyName: [
        //     { required: true, message: "请输入类别名称", trigger: "blur" },
        //   ],
      },
      loading: false,
    };
  },
  mounted() {
    const ls_VarietylD = sessionStorage.getItem("VarietylD");

    this.getData(ls_VarietylD);
  },
  methods: {
    dhandleRemove(file, index) {
      console.log(file, index);
      this.upLoadFilesList[index] = new Array();
      this.$set(this, "upLoadFilesList", [...this.upLoadFilesList]);
      console.log(this.upLoadFilesList);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
      console.log(this.fmfileList);
      this.fmfileList.splice(0, 1);
      // console.log(this.VarietyName);
      const ls_VarietylD = sessionStorage.getItem("VarietylD");
      console.log(ls_VarietylD);

      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
        type: "post",
        data: {
          Data: "base_EdibleMushroomVariety", //表名
          Condition: "Row_Status='0' And VarietyID = '" + ls_VarietylD + "'",
        },
        success: function (Result) {
          console.log(Result);
          Result[0].Show_image = "";
          console.log(Result);
          let lJSONObject_base_EdibleMushroomVariety = new Object();
          lJSONObject_base_EdibleMushroomVariety.base_EdibleMushroomVariety =
            Result; //换表
          let lJSONObject_DataSet = new Object(); //Modify
          lJSONObject_DataSet.Modify = lJSONObject_base_EdibleMushroomVariety; //换表
          console.log(lJSONObject_DataSet);

          // return;
          $.ajax({
            // headers: requestHeaders(),
            // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
            url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
            type: "POST",
            data: {
              Data: JSON.stringify(lJSONObject_DataSet),
            },
            success: function (Result) {
              console.log(Result);
            },
            complete: function (XMLHttpRequest) {
              requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
            },
            dataType: "json",
          });
        },
        complete: function (XMLHttpRequest) {
          // requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    dhandleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
    },
    // dhandleBeforeUpload(file, index) {
    //   // console.log(file, fileList);
    //   // if (index == undefined) return;
    //   // 生成预览URL并添加到fileList中
    //   const reader = new FileReader();
    //   reader.onload = (e) => {
    //     const newFile = {
    //       name: file.name,
    //       url: e.target.result, // 预览URL
    //       uid: file.uid,
    //       // 可以添加其他属性，如上传状态等
    //     };
    //     // let li_ArraySize = this.upLoadFilesList[index].length;
    //     const fileList =
    //       typeof this.upLoadFilesList[index] == "Array"
    //         ? this.upLoadFilesList[index]
    //         : [];
    //     fileList.push(newFile);
    //     this.upLoadFilesList[index] = fileList;

    //     const _this = this;
    //     $.ajax({
    //       // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
    //       url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
    //       // url: "http://192.168.10.79:8087/store/photo",
    //       // url: "http://192.168.10.79:8087/store/photo",
    //       type: "POST", // 注意HTTP方法应该大写
    //       contentType: "application/json", // 设置请求体的媒体类型为application/json
    //       data: JSON.stringify(this.upLoadFilesList[index]), // 将数据序列化为JSON字符串
    //       success: function (Result) {
    //         // 1.表单赋值
    //         let base64Image = _this.upLoadFilesList[index][0].url;
    //         let dataIndex = base64Image.indexOf("data:");
    //         let base64Index = base64Image.indexOf(";base64,");
    //         let substring = base64Image.substring(dataIndex + 5, base64Index);
    //         let ls_split = substring.split("/");
    //         // 2.展示图片所用数组赋值
    //         const obj = {
    //           ..._this.upLoadFilesList[index][0],
    //           status: "success",
    //           url: `https://www.kdddy.cn/Image/${
    //             _this.upLoadFilesList[index][0].uid + "." + ls_split[1]
    //           }`,
    //         };
    //         console.log("obj", obj);
    //         _this.upLoadFilesList[index] = [obj];
    //         const formchild = {
    //           ..._this.form2[index],
    //           Show_image:
    //             _this.upLoadFilesList[index][0].uid + "." + ls_split[1],
    //         };
    //         _this.form2[index] = formchild;

    //         console.log(333, _this.upLoadFilesList);
    //         // _this.fmfileList = lArray_URL;
    //         // upLoadFilesList.push(new Array());
    //         // let lJSONObject_business_article_pic = new Object();
    //         // lJSONObject_business_article_pic.business_article_pic =
    //         //   _this.upLoadFilesList[index];

    //         // _this.form2[index] = _this.upLoadFilesList[index];
    //         // _this.upLoadFilesList[index];
    //       },
    //       complete: function (XMLHttpRequest) {
    //         requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
    //       },
    //       dataType: "json", // 预期服务器返回的数据类型
    //     });
    //   };
    //   reader.readAsDataURL(file);
    //   // console.log(this.fmfileList);

    //   // 返回false以阻止自动上传
    //   return false;
    // },
    dhandleBeforeUpload(file, index) {
      // console.log(file, fileList);
      // if (index == undefined) return;
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          // 可以添加其他属性，如上传状态等
        };
        this.upLoadFilesList[index] = new Array();
        console.log(this.upLoadFilesList[index]);
        this.upLoadFilesList[index].push(newFile);
        this.$set(this, "upLoadFilesList", [...this.upLoadFilesList]);
        // this.upLoadFilesList[index] =
        //   typeof this.upLoadFilesList[index] == "Array"
        //     ? this.upLoadFilesList[index]
        //     : [];
        // console.log("uuu", newFile);
        // this.upLoadFilesList[index].push(newFile);
        console.log(888, this.upLoadFilesList);

        // console.log(newFile);
        const _this = this;
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.upLoadFilesList[index]), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
            console.log(_this.upLoadFilesList[index]);

            let base64Image = _this.upLoadFilesList[index][0].url;
            let dataIndex = base64Image.indexOf("data:");
            let base64Index = base64Image.indexOf(";base64,");
            let substring = base64Image.substring(dataIndex + 5, base64Index);
            let ls_split = substring.split("/");
            _this.form2[index].Show_image =
              _this.upLoadFilesList[index][0].uid + "." + ls_split[1];
            // upLoadFilesList.push(new Array());
            // let lJSONObject_business_article_pic = new Object();
            // lJSONObject_business_article_pic.business_article_pic =
            //   _this.upLoadFilesList[index];

            // _this.form2[index] = _this.upLoadFilesList[index];
            // _this.upLoadFilesList[index];
            console.log(111, _this.form2[index]);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    showFileList: function (index) {
      // return function (url) {
      //   return url ? [url] : [];
      // };
      console.log("index", index, this.upLoadFilesList[index]);

      return this.upLoadFilesList[index];
    },
    chandleChange(file, fileList) {
      // 这个方法现在主要用于调试或处理其他上传后的逻辑
      // 注意：这里的fileList参数与:file-list绑定的fileList不是同一个对象
      // :file-list绑定的fileList是组件内部维护的，而这里的fileList是上传事件提供的
      console.log("Upload change event:", file, fileList);
    },
    handleBeforeUpload(file, fileList) {
      console.log(file, fileList);

      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid,
          Add: 1,
          // 可以添加其他属性，如上传状态等
        };
        this.fmfileList.push(newFile);
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fmfileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); // 确保这个函数在config/config.js中定义
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // console.log(this.fmfileList);

      // 返回false以阻止自动上传
      return false;
    },
    add_form2() {
      const ls_VarietylD = sessionStorage.getItem("VarietylD");
      this.form2.push({
        GradeName: "",
        Status: "Add",
        VarietyID: ls_VarietylD,
        Show_image: "",
        Status: "Add",
      });
      // this.upLoadFilesList[
      //   this.upLoadFilesList?.length ? this.upLoadFilesList?.length - 1 : 0
      // ] = new Array();
      // console.log(this.upLoadFilesList);

      for (let i = 0; i < this.form2.length; i++) {
        if (this.form2[i].Status == "Add") {
          this.form3.push(this.form2[i]);
        } else continue;
      }

      console.log(this.form2);
      console.log("-----------------------------------------");

      console.log(this.form3);
    },
    delet_form2(index) {
      if (index == 0) {
        this.$message({
          message: "不能在删了",
          type: "warning",
        });
      } else {
        const _this = this;
        console.log(index);
        // return;
        const ls_VarietylD = sessionStorage.getItem("VarietylD");
        console.log(this.form2);
        this.form2[index].Row_Status = "1";

        // delete this.form2[index].Operation_Datetime;
        // delete this.form2[index].Operation_Datetime_Original;
        let lJSONObject_base_EdibleMushroomGrade = new Object();
        lJSONObject_base_EdibleMushroomGrade.base_EdibleMushroomGrade =
          this.form2; //换表
        let lJSONObject_DataSet = new Object(); //Modify
        lJSONObject_DataSet.Modify = lJSONObject_base_EdibleMushroomGrade; //换表
        // return;
        // $.ajax({
        //   headers: requestHeaders(),
        //   type: "POST",
        //   // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        //   url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
        //   data: {
        //     Data: "base_EdibleMushroomGrade", //表名
        //     Condition: "VarietyID='" + ls_VarietylD + "'",
        //   },
        //   success: function (Result) {
        //     console.log(Result);
        //     console.log(Result[index]);
        //     Result[index].Row_Status = 1;

        //     let lArray = new Array();
        //     lArray.push(Result[index]);
        //     let lJSONObject_base_EdibleMushroomGrade = new Object();
        //     lJSONObject_base_EdibleMushroomGrade.base_EdibleMushroomGrade =
        //       lArray; //换表
        //     let lJSONObject_DataSet = new Object(); //Modify
        //     lJSONObject_DataSet.Modify = lJSONObject_base_EdibleMushroomGrade; //换表
        console.log(lJSONObject_DataSet);
        // return;
        $.ajax({
          // headers: requestHeaders(),
          // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
          url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
          type: "POST",
          data: {
            Data: JSON.stringify(lJSONObject_DataSet),
          },
          success: function (Result) {
            console.log(Result);
            _this.form2.splice(index, 1);
            // if (Result.Status === "success") {
            //   this.$message.success("提交成功");
            //   window.history.go(-1);
            // } else {
            //   this.$message.error("提交失败，请稍后再试");
            // }
          },
          complete: function (XMLHttpRequest) {
            requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
          },
          dataType: "json",
        });

        //   that.total = Result.TotalRows;
        // },
        // complete: function (XMLHttpRequest) {
        //   requestPretreatment(XMLHttpRequest);
        // },
        // dataType: "json",
        // });
      }
    },
    getData(Category_SerialNumber) {
      const _this = this;
      //子表带原值
      $.ajax({
        url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
        type: "post",
        data: {
          Data: "base_EdibleMushroomVariety", //表名
          Condition: "VarietyID='" + Category_SerialNumber + "'",
        },
        success: function (data) {
          console.log(data, "data");
          let lArray_URL = new Array();
          let lJSONObject_Picture_Record = new Object();
          lJSONObject_Picture_Record.url = `https://www.kdddy.cn/Image/${data[0]?.Show_image}`;
          lArray_URL.push(lJSONObject_Picture_Record);
          console.log(lArray_URL);

          _this.fmfileList = lArray_URL;
          _this.form = data[0];
          console.log(_this.form);

          //   console.log(data.Main[0].VarietyName, "VarietyName");
          // console.log(_this.form, "this.form.VarietyName");
          // console.log(data.Main);
          _this.VarietyName = data[0].VarietyName;

          $.ajax({
            headers: requestHeaders(),
            type: "POST",
            // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
            url: "https://service.kdddy.cn/donghui/generalBusiness/generalQueryByTableNameWithOriginal",
            data: {
              Data: "base_EdibleMushroomGrade", //表名
              Condition:
                "VarietyID='" +
                Category_SerialNumber +
                "' And Row_Status='" +
                "0" +
                "'",
            },
            success: function (Result) {
              console.log(Result);

              // _this.upLoadFilesList = Result;
              console.log(777, _this.upLoadFilesList, Result);

              for (let i = 0; i < Result.length; i++) {
                let lArray_URL = new Array();
                let lJSONObject_Picture_Record = new Object();
                lJSONObject_Picture_Record.url = `https://www.kdddy.cn/Image/${Result[i].Show_image}`;
                lArray_URL.push(lJSONObject_Picture_Record);
                console.log("url", lArray_URL);
                if (i == 0) {
                  _this.upLoadFilesList[i] = lArray_URL;
                } else {
                  _this.upLoadFilesList.push(new Array());
                  _this.upLoadFilesList[i] = lArray_URL;
                }
              }
              console.log(lArray_URL);

              _this.form2 = Result;
            },
            complete: function (XMLHttpRequest) {
              //   requestPretreatment(XMLHttpRequest);
            },
            dataType: "json",
          });

          _this.form2 = data.child;
          console.log("----------------------------------");
          console.log(_this.form);

          //   this.form.VarietyName = data.Main[0].VarietyName;
        },
        complete: function (XMLHttpRequest) {},
        dataType: "json",
      });
    },
    submitForm(formName) {
      //500
      const _this = this;
      const ls_VarietylD = sessionStorage.getItem("VarietylD");
      _this.form.VarietyID = ls_VarietylD;
      _this.form.VarietyName = _this.VarietyName;
      delete _this.form.Operation_Datetime;
      delete _this.form.Operation_Datetime_Original;

      this.fmfileList?.filter((i) => {
        if (i.Add) {
          let base64Image = _this.fmfileList[0].url;
          let dataIndex = base64Image.indexOf("data:");
          let base64Index = base64Image.indexOf(";base64,");
          let substring = base64Image.substring(dataIndex + 5, base64Index);
          let ls_split = substring.split("/");
          _this.form.Show_image = _this.fmfileList[0].uid + "." + ls_split[1];
        }
      });

      let lJSONArray_Modify_base_EdibleMushroomGrade = new Array();
      let lJSONArray_Add_base_EdibleMushroomGrade = new Array();
      for (let i = 0; i < _this.form2.length; i++) {
        if (_this.form2[i].Status === "Add") {
          lJSONArray_Add_base_EdibleMushroomGrade.push(_this.form2[i]);
        } else {
          lJSONArray_Modify_base_EdibleMushroomGrade.push(_this.form2[i]);
        }
      }
      console.log(lJSONArray_Add_base_EdibleMushroomGrade);
      console.log(lJSONArray_Modify_base_EdibleMushroomGrade);
      lJSONArray_Modify_base_EdibleMushroomGrade?.forEach((element) => {
        console.log(element);
        delete element.Operation_Datetime;
        delete element.Operation_Datetime_Original;
      });
      console.log(lJSONArray_Modify_base_EdibleMushroomGrade);
      console.log(_this.form2);
      console.log(_this.form);
      console.log(this.fmfileList);
      let lArray = new Array();
      lArray.push(_this.form);
      let lJSONObject_base_EdibleMushroomVariety = new Object();
      lJSONObject_base_EdibleMushroomVariety.base_EdibleMushroomVariety =
        lArray; //换表
      lJSONObject_base_EdibleMushroomVariety.base_EdibleMushroomGrade =
        lJSONArray_Modify_base_EdibleMushroomGrade;
      let lJSONObject_base_EdibleMushroomGrade = new Object();
      lJSONObject_base_EdibleMushroomGrade.base_EdibleMushroomGrade =
        lJSONArray_Add_base_EdibleMushroomGrade; //换表
      let lJSONObject_DataSet = new Object(); //Modify
      lJSONObject_DataSet.Modify = lJSONObject_base_EdibleMushroomVariety; //换表
      lJSONObject_DataSet.Add = lJSONObject_base_EdibleMushroomGrade;
      console.log(lJSONObject_DataSet);
      // return;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message.success("提交成功");
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
});
